package com.example.spinning.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spinning.bean.DyeingInfo;
import com.example.spinning.bean.PaginationResult;
import com.example.spinning.mapper.DyeingInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author bayernxmt
 * @create 2020-11-11-11:13
 */
@Service
@Transactional
public class DyeingInfoService {
    @Autowired
    private DyeingInfoMapper dyeingInfoMapper;

    //查询所有 并且分页展示 : current表示当前页，size表示一页展示多少条数据
    public PaginationResult<DyeingInfo> selectAll(int current, int size){
        Page<DyeingInfo> pages = new Page<>(current,size);
        dyeingInfoMapper.selectPage(pages, null);
        List<DyeingInfo> list = pages.getRecords();
        int total = dyeingInfoMapper.selectCount(null);

        PaginationResult<DyeingInfo> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //单、多条件查询 并且实现分页
    public PaginationResult<DyeingInfo> selectByMulParams(int current, int size, DyeingInfo dyeingInfo){

        QueryWrapper<DyeingInfo> wrapper = new QueryWrapper();
        if (null!=dyeingInfo.getDyeing_id())
            wrapper.eq("dyeing_id", dyeingInfo.getDyeing_id());

        Page<DyeingInfo> pages = new Page<>(current,size);
        dyeingInfoMapper.selectPage(pages, wrapper);
        int total = dyeingInfoMapper.selectCount(wrapper);
        List<DyeingInfo> list = pages.getRecords();

        PaginationResult<DyeingInfo> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }

    //根据id查询
    public DyeingInfo SelectById(int id){
        return dyeingInfoMapper.selectById(id);
    }

    //插入数据
    public int insert(DyeingInfo dyeingInfo) {
        int result = dyeingInfoMapper.insert(dyeingInfo); //result为受到影响的条数
        return result;
    }


    //批量删除数据（与删除一条合并）
    public int delete(List<Integer> ids){
        int result = dyeingInfoMapper.deleteBatchIds(ids);
        return result;
    }

    //修改数据(已经实现了动态sql)
    public int update(DyeingInfo dyeingInfo){
        int result = dyeingInfoMapper.updateById(dyeingInfo);
        return result;
    }
}
